import Taro from '@tarojs/taro';
import { View, Text, Image, Picker } from '@tarojs/components';
import Survey from '@/components/survey/index'
import http from '@/utils/http';
import utils from '@/utils/util'

import './index.scss';

export default class Signin extends Taro.Component {

    constructor(props) {
        super(props);
        const now = new Date();
        this.state = {
            wardArr: [],
            Medicine: {},
            MedicalkitId: '',
            nowtime: `${now.getHours() < 10 ? `0${now.getHours()}` : now.getHours()}:${now.getMinutes() < 10 ? `0${now.getMinutes()}` : now.getMinutes()}`
        }
    }
    componentWillUnmount() {
        clearInterval(this.timerID);
    }
    config = {
        navigationBarTitleText: '巡检签到'
    }
    componentDidShow () {
        this.setState({
            MedicalkitId: this.$router.params.MedicalkitId
        },()=>{
            this.getAllwards();
            this.getMedicalkitById();
            this.timerID = setInterval(
                () => this.tick(),
                1000
            );
        })
    }
    tick() {
        const now = new Date();
        this.setState({
            nowtime: `${now.getHours() < 10 ? `0${now.getHours()}` : now.getHours()}:${now.getMinutes() < 10 ? `0${now.getMinutes()}` : now.getMinutes()}`
        });
    }
    getMedicalkitById() {
        const { MedicalkitId } = this.state;
        http.get('getMedicalkitById', {
            MedicalkitId
        }).then((d) => {
            this.setState({
                Medicine: d.datas
            })
        }).catch((e) => {
            setTimeout(() => {
                utils.showtoast(e.datas || '获取药品信息失败，请稍后重试',undefined,undefined,true);
            })
            setTimeout(()=>{
                Taro.navigateBack();
            },1500)
        })
    }
    getAllwards() {
        http.get('getAllwards').then((d) => {
            this.setState({
                 wardArr: d.datas
            })
        }).catch((e) => {
            console.log(e)
        })
    }
    onChange(e) {
        this.setState((prevState, props) => ({
            Medicine: {
                ...prevState.Medicine,
                wardid: prevState.wardArr[e.detail.value].wardid,
                wardname: prevState.wardArr[e.detail.value].wardname
            }
        }))
    }
    render() {
        const { nowtime, Medicine, MedicalkitId } = this.state;
        return (
            <View className='signin box_s pt-2-5 pl-2-5 pr-2-5'>
                <View className='bdr_10 ov_h fle'>
                    <View className='signin-center box_s ft26'>
                        <View className='pl-3-5  pt-3 pb-3  pr-3-5 box_s line-b flex flex-r  bg_fff'>
                            <View className='flex flex-r flex-item-v-center'>
                                <Image src={require('../../assets/imgs/sigin/xjqd_icon_xjsj.png')} className='icon-box'></Image>
                                <Text className='fc_333 pl-2'>巡检时间：{nowtime}</Text>
                            </View>
                            <View className='flex flex-r flex-item-v-center pl-9-5 '>
                                <Image src={require('../../assets/imgs/sigin/xjqd_icon_dqks.png')} className='icon-box'></Image>
                                <Text className='fc_333 pl-2'>当前科室：{Medicine.departmentname}</Text>
                            </View>
                        </View>
                        <View className='bg_fff pl-3-5  pt-3 pb-3  pr-3-5 box_s line-b flex flex-r flex-item-between flex-item-v-center'>
                            <Text className='fc_53C'>存放地点：{Medicine.wardname}</Text>
                            <View>
                                <Picker mode='selector' range={this.state.wardArr} rangeKey='wardname' onChange={(event) => { this.onChange(event) }}>
                                    <Text className='fc_960 ft24' >地点有误</Text>
                                </Picker>
                            </View>
                        </View>
                    </View>
                    <Survey isTop={false} medicalkit={Medicine}></Survey>
                </View>
                <View onClick={() => { Taro.navigateTo({ url: `/pages/inspectionSubmit/index?wardid=${Medicine.wardid}&wardname=${Medicine.wardname}&MedicalkitId=${MedicalkitId}`})}} className='signin-tap flex flex-item-center flex-c fc_FFF mt-18 mt-auto'>
                    <View className='ft78'>{nowtime}</View>
                    <View className='ft34'>签到</View>
                </View>
                <View className='ft28 text-c mt-3-5' style={{ color: '#868B9C' }}>今日{Medicine.ischecktoday ? "已" : "还未"}签到</View>
                <View className='ft28 text-c mt-5 fc_1FF' ><Text className='ft28 mt-auto'
                  onClick={() => { Taro.navigateTo({ url: `/pages/signRecord/index` }) }}
                >签到记录</Text></View>
            </View>
        );
    }
}